package com.eshore.norm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eshore.norm.mapper.NormRegionMapper;
import com.eshore.norm.pojo.constant.Const;
import com.eshore.norm.pojo.dto.NormRegion;
import com.eshore.norm.pojo.po.NormRegionsPO;
import com.eshore.norm.service.INormRegionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 区域表 服务实现类
 * </p>
 *
 * @author chenjl
 * @since 2024-01-05
 */
@Slf4j
@Service
public class NormRegionServiceImpl extends ServiceImpl<NormRegionMapper, NormRegion> implements INormRegionService {

    @Override
    public void dataImport(NormRegionsPO po)    {
        log.info("===>3333po: "+po);
        List<NormRegionsPO.ContentDTO.RegionListDTO> regionList = po.getContent().get(0).getRegionList();
        if (CollectionUtils.isNotEmpty(regionList)){
            List<NormRegion> regions = handleRegion(Const.CONFIG_ID, regionList);
            log.info("===>regions: "+regions);
            List<String> newIds = regions.stream().map(NormRegion::getRegionName).collect(Collectors.toList());
            List<String> delIds = cleanRegionOldData(Const.CONFIG_ID, newIds);
            getBaseMapper().insertOrUpdate(regions);
            //信息同步到cmdb
           // syncRegionCmdb(regions,delIds);
        }

    }


//    //信息同步到cmdb
//    private void syncRegionCmdb(List<NormRegion> regionList, List<String> oldRegionIds) {
//        if (CollectionUtils.isNotEmpty(regionList)) {
//            List<CmdbRegion> dtos = regionList.stream().map(t -> {
//                CmdbRegion region = new CmdbRegion();
//                region.setId(t.getId());
//                region.setAccountId(t.getConfigId());
//                region.setRegionId(t.getRegionName());
//                region.setName(t.getRegionRemark());
//                region.setCloudType(CloudTypeEnum.PRIVATE);
//                region.setCreateDate(t.getCreateDate());
//                region.setUpdateDate(t.getUpdateDate());
//                return region;
//            }).collect(Collectors.toList());
//            //同步到cmdb
//            SyncMessageContent<CmdbRegion> messageContent = new SyncMessageContent<>();
//            messageContent.setCloudType(SyncMessageContent.CLOUD_TYPE_NORM_CLOUD);
//            messageContent.setResourceType(SyncMessageContent.RESOURCE_TYPE_SYNC_REGION);
//            messageContent.setAddOrUpdateDatas(dtos);
//            messageContent.setRemoveStrIds(oldRegionIds);
//            rabbitService.defaultDirectSend(SyncMessageContent.QUEUE_NAME, JSONObject.toJSONString(messageContent));
//        }
//    }


    private List<String> cleanRegionOldData(Long configId, List<String> newIds) {
        //查出原来的旧数据
        log.info("newIds:"+newIds);
        List<String> oldRegionIds = lambdaQuery().eq(NormRegion::getConfigId, configId).list().stream().map(NormRegion::getRegionName).collect(Collectors.toList());
        oldRegionIds.removeAll(newIds);
        log.info("oldRegionIds:"+oldRegionIds);
        if (CollectionUtils.isNotEmpty(oldRegionIds)) {
            log.info("进入delete方法");
            getBaseMapper().delete(new QueryWrapper<NormRegion>().in("region_name", oldRegionIds));
            return oldRegionIds;
        }
        return null;
    }

    private List<NormRegion> handleRegion(Long configId,List<NormRegionsPO.ContentDTO.RegionListDTO> regions){
        List<NormRegion> result = new ArrayList<>();
        regions.forEach(r -> {
            NormRegion region = new NormRegion();
            region.setId(IdWorker.getId());
            region.setConfigId(configId);
            region.setCreateDate(new Date());
            region.setUpdateDate(new Date());
            region.setRegionName(r.getRegionName());
            region.setRegionRemark(r.getRegionRemark());
            result.add(region);
        });
        return result;
    }
}
